package com.mdd.admin.controller;

import com.mdd.admin.service.IBannerService;
import com.mdd.admin.validate.banner.BannerCreateValidate;
import com.mdd.admin.validate.banner.BannerSearchValidate;
import com.mdd.admin.validate.banner.BannerUpdateValidate;
import com.mdd.admin.vo.banner.BannerDetailVo;
import com.mdd.admin.vo.banner.BannerListedVo;
import com.mdd.common.aop.Log;
import com.mdd.common.aop.NotPower;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validate.IdValidate;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@RestController
@RequestMapping("api/banner")
@Api(tags = "广告数据管理")
public class BannerController {

    @Resource
    IBannerService iBannerService;

    @GetMapping("/list")
    @ApiOperation(value="广告列表")
    @NotPower
    public AjaxResult<PageResult<BannerListedVo>> list(@Validated PageValidate pageValidate,
                                                       @Validated BannerSearchValidate searchValidate) {
        PageResult<BannerListedVo> vos = iBannerService.list(pageValidate, searchValidate);
        return AjaxResult.success(vos);
    }

    @GetMapping("/detail")
    @ApiOperation(value="广告详情")
    @NotPower
    public AjaxResult<BannerDetailVo> detail(@Validated @IDMust() @RequestParam("id") Long id) {
        BannerDetailVo vo = iBannerService.detail(id);
        return AjaxResult.success(vo);
    }

    @Log(title = "广告新增")
    @PostMapping("/add")
    @ApiOperation(value="广告新增")
    @NotPower
    public AjaxResult<Object> add(@Validated @RequestBody BannerCreateValidate createValidate) {
        iBannerService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "广告编辑")
    @PostMapping("/edit")
    @ApiOperation(value="广告编辑")
    @NotPower
    public AjaxResult<Object> edit(@Validated @RequestBody BannerUpdateValidate updateValidate) {
        iBannerService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "广告删除")
    @PostMapping("/del")
    @ApiOperation(value="广告删除")
    @NotPower
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iBannerService.del(idValidate.getId());
        return AjaxResult.success();
    }
}
